import { S, U, getRequestId, mapField } from '../../lib/backend'
import Q from 'q'
import axios from 'axios'
import env from '../../config/common'
import { UPDATE_DATA_DICTCTIONARY, UPDATE_SWITCH_SITE_STATUS } from '../mutation-types'

export default {
  // 获取客户列表
  getSources ({ dispatch, commit, state, rootState }, info) {
    const frag = '?' + U(info)
    return Q.Promise((success, error) => {
      axios.get(
        env.CRM_API_HOST + '/users/' + rootState.auth.user.token + '/sources' + frag
      ).then((response) => {
        if (response.data.results) {
          const sources = response.data.results
          response.data.results.data = sources
        }
        success(response)
      }).catch((err) => {
        error(err)
        console.log(err)
      })
    })
  },
  getSource ({ dispatch, commit, state, rootState }, info) {
    const frag = '?' + U()
    let idParam = ''
    if (typeof info.id !== 'undefined') {
      idParam = '/' + info.id
    }
    return Q.Promise((success, error) => {
      axios.get(
        env.CRM_API_HOST + '/users/' + rootState.auth.user.token + '/sources' + idParam + frag
      ).then((response) => {
        success(response)
      }).catch((err) => {
        error(err)
        console.log(err)
      })
    })
  },
  saveSource ({ dispatch, commit, state, rootState }, info) {
    const params = S({
      trace: { requestId: getRequestId() },
      body: info
    })
    let idParam = ''
    if (typeof info.id !== 'undefined') {
      idParam = '/' + info.id
    }
    return Q.Promise((success, error) => {
      axios.post(
        env.CRM_API_HOST + '/users/' + rootState.auth.user.token + '/sources' + idParam,
        params
      ).then((response) => {
        if (response.data.results) {
          const sources = response.data.results
          response.data.results.data = sources
        }
        success(response)
      }).catch((err) => {
        error(err)
        console.log(err)
      })
    })
  },
  deleteSource ({ commit, state, rootState }, info) {
    const API_HOST = env.CRM_API_HOST
    const frag = '/' + info.id
    // const API_HOST = 'http://120.24.48.9:8889/http://rapapi.org/mockjsdata/17991/'
    return Q.Promise((success, error) => {
      axios.delete(
         API_HOST + '/users/' + rootState.auth.user.token + '/sources' + frag
      ).then((response) => {
        success(response)
      }).catch((err) => {
        error(err)
        console.log(err)
      })
    })
  },
  // 获取数据字典
  getDictionary ({ dispatch, commit, state, rootState }, info) {
    const frag = '?' + U(info)
    return Q.Promise((success, error) => {
      if (typeof rootState.source['data_dictionary'] !== undefined) {
        success(rootState.source['data_dictionary'])
      }
      axios.get(
        env.CRM_API_HOST + '/data_dictionary' + frag
      ).then((response) => {
        if (response.data.errcode === 0) {
          const data = response.data.results
          response.data.results = data
          commit(UPDATE_DATA_DICTCTIONARY, data)
        }
        success(response)
      }).catch((err) => {
        error(err)
        // console.log(err)
      })
    }).catch((error) => {
      console.warn('error')
    })
  },

  switchSource ({ dispatch, commit, state, rootState }, info) {
    let frag = '?' + U()
    const params = S(info)
    if (typeof info['source_id'] !== 'undefined') {
      frag = '/' + info['source_id']
    }
    return Q.Promise((success, error) => {
      axios.get(
        env.CRM_API_HOST + '/users/' + rootState.auth.user.token + '/switch_source' + frag,
        params
      ).then((response) => {
        success(response)
      }).catch((err) => {
        error(err)
        console.log(err)
      })
    })
  },
  showSwitchSource ({ commit }, info) {
    commit(UPDATE_SWITCH_SITE_STATUS, true)
  },

  hideSwitchSource ({ commit }, info) {
    commit(UPDATE_SWITCH_SITE_STATUS, false)
  }
}
